const app = getApp();
var qCloud = require("../../index.js");
var timerInterval = null;

Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        phone: "",
        password: "",
        confirmPassword: "",

        codeDisabled: false,
        codeText: "获取验证码",
        countDown: 60,
        maxTime: 60,
    },

    // 监听输入手机号
    phoneInput(e) {
        this.setData({
            phone: e.detail.value
        })
    },

    // 监听短信验证码
    codeInput: function (e) {
        this.data.code = e.detail.value;
    },

    // 监听输入密码
    passwordInput(e) {
        this.setData({
            password: e.detail.value
        })
    },

    // 监听输入确认密码
    passwordConfirmInput(e) {
        this.setData({
            confirmPassword: e.detail.value
        })
    },

    // 发送短信验证码
    bindSendCodeClick() {
        let self = this;
        if (self.data.phone == "") {
            wx.showToast({
                title: "请输入手机号码",
                icon: "error"
            })
            return;
        }
        let params = {
            type: "0",
            phone: qCloud.crypto.encrypt(self.data.phone)
        };

        // 发送验证码
        app.fetchNoTokenApis(self, qCloud.api.smsCode, params, "POST", function (e) {
            if (e.data.code != 200) {
                self.data.codeDisabled = false
                if (timerInterval) {
                    clearTimeout(timerInterval);
                }
                wx.showModal({
                    content: e.data.msg
                });
            } else {
                self.data.codeText = self.data.maxTime + "s后重试";
                self.data.codeDisabled = true;
                timerInterval = setTimeout(self.refreshCode, 1000);
            }
        })
    },

    // 刷新短信验证码发送倒计时
    refreshCode: function () {
        let self = this;
        if (self.data.countDown > 0) {
            self.setData({
                codeDisabled: true,
                codeText: self.data.countDown + "s后重试",
                countDown: self.data.countDown - 1
            })
            timerInterval = setTimeout(self.refreshCode, 1000);
        } else {
            self.setData({
                codeDisabled: false,
                codeText: "重新获取",
                countDown: self.data.maxTime
            })
            if (timerInterval) {
                clearTimeout(timerInterval);
            }
        }
    },

    // 确认重置密码
    bindUpdatePassword() {
        let self = this;
        if (self.data.phone == "") {
            wx.showToast({
                title: "请输入手机号码",
                icon: "error"
            })
            return;
        }

        if (self.data.password == "") {
            wx.showToast({
                title: "请输入密码",
                icon: "error"
            })
            return;
        }

        if (self.data.confirmPassword == "") {
            wx.showToast({
                title: "请输入确认密码",
                icon: "error"
            })
            return;
        }

        if(self.data.password != self.data.confirmPassword){
            wx.showToast({
                title: "请输入输入密码与确认密码不一致",
                icon: "error"
            })
            return;
        }

        let params = {
            phone: qCloud.crypto.encrypt(self.data.phone),
            code: self.data.code,
            password: qCloud.crypto.encrypt(self.data.password)
        };

        // 发送验证码
        app.fetchNoTokenApis(self, qCloud.api.forget, params, "POST", function (e) {
            if (e.data.code == 200) {
                wx.showModal({
                    title: '账号密码重置成功',
                    confirmText: '重新登录',
                    success: function () {
                        wx.redirectTo({
                            url: "/pages/login/index"
                        })
                    }
                })
            } else {
                qCloud.util.showSelfModal(self, e.data.msg);
            }
        })

    },
})
